﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DAL;
using System.Data;

/// <summary>
///gaInfoNews 的摘要说明
/// </summary>
public class gaInfoNews
{
	public gaInfoNews()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public List<News> GetNews()
    {
        List<News> list = new List<News>(); 
        string sql = "select top 100 id, Title,Date,[Content] from News where IsCheck=0";
        DataTable tb = DAL.SqlHelper.ExecuteGetTable(DBManager.GaInfoStrCon, System.Data.CommandType.Text, sql, null);
        foreach (DataRow row in tb.Rows)
        {
            News news = new News();
            news.N_id = Tools.GetInt(row["id"]);
            news.N_title = Tools.GetString(row["Title"]);
            news.N_cTime = Tools.GetDateTime(row["Date"]);
            news.N_content = Tools.GetString(row["Content"]);
            list.Add(news);
        }
        return list;
    }
    public bool DeleteNewsById(int id)
    {
        string sql = "delete news where id = @id ";
        Tools tool = new Tools();
        tool.AddSqlParameter("@id",SqlDbType.Int,id);
        if (SqlHelper.ExecuteNonQuery(DBManager.GaInfoStrCon, CommandType.Text, sql, tool.GetSqlParameters()) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool UpdateNewsById(News news)
    {
        Tools tool = new Tools();
        string sql = "update news set title=@title,content=@content,date=@date where id=@id";
        tool.AddSqlParameter("@id", SqlDbType.VarChar, news.N_id);
        tool.AddSqlParameter("@title", SqlDbType.VarChar, news.N_title);
        tool.AddSqlParameter("@Content", SqlDbType.VarChar, news.N_content);
        tool.AddSqlParameter("@Date", SqlDbType.DateTime, news.N_cTime);

        if (SqlHelper.ExecuteNonQuery(DBManager.GaInfoStrCon, CommandType.Text, sql, tool.GetSqlParameters()) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool UpdateNewsCheckById(int id)
    {
        Tools tool = new Tools();
        string sql = "update news set ischeck=1 where id=@id";
        tool.AddSqlParameter("@id", SqlDbType.VarChar, id);

        if (SqlHelper.ExecuteNonQuery(DBManager.GaInfoStrCon, CommandType.Text, sql, tool.GetSqlParameters()) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public News GetNewsById(int id)
    {
        try
        {
            News news = GetNews().Single(x => x.N_id == id);
            return news;
        }
        catch (Exception e)
        {
            return null;
        }
    }
    //实体类News
    public class News
    {
        /// <summary>
        /// newsId
        /// </summary>
        public int? N_id { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? N_cTime { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string N_title { get; set; }
        /// <summary>
        /// 新闻内容
        /// </summary>
        public string N_content { get; set; }
    }
}